package cn.serviceImpl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import cn.service.CommonService;
import cn.utils.PageModel;



public class CommonServiceImpl extends BaseService implements CommonService {

	//@Override
	public Double Stasistics(String hql) {

		return (Double) this.commonDao.Stasistics(hql);
	}

	//@Override
	public void deleteDemo(Object obj ,Object[] id) {

		if(null!=id){

			for(int i=0;i<id.length;i++){
				String temp=(String)id[i];
				Object ob=this.commonDao.findByStringId(temp, obj.getClass());
				this.commonDao.deleteDemo(ob);
			}
		}

		this.commonDao.deleteDemo(obj);
	}


	//public <T extends Serializable> T findByIntId(int id, T t) {

	//	return this.commonDao.findByIntId(id, t);
	//}

	//@Override
	public <T extends Serializable> T findById(Object id, T t) {

		if (id instanceof String) {
			return this.commonDao.findByStringId((String)id, t);
		}
		if(id instanceof Integer){
			return this.commonDao.findByIntId((Integer)id, t);
		}
		return null;

	}

	//@Override
	public PageModel findDataByPage(PageModel pageModel,String tableName, List listOne,
			String conOneTwo, List listTwo, String conTwoThree, List listThree,
			String conThreeFour, List listFour,
			String[] orderPropertyName, String[] orderValue) {
		 String hql=" ";
		 String temp1=null;
	     String temp2=null;
		 String temp3=null;
		 String temp4=null;
		if(null==tableName) return null;
		hql=" from "+ tableName +" o ";
		if(null!=listOne){
		    temp1=this.assemblyHQLCondition(listOne);
			if(null!=temp1 && !"".equals(temp1))
				hql+=" where "+temp1+" ";
		}
		if(null!=listTwo){
			temp2=this.assemblyHQLCondition(listTwo);
			if(null!=temp2 && !"".equals(temp2))
				if(null==temp1||"".equals(temp1)){
					hql+=" where "+temp2+" ";
				}else
				hql+=conOneTwo+" "+temp2+" ";
		}
		if(null!=listThree){
			temp3=this.assemblyHQLCondition(listThree);
			if(null!=temp3 && !"".equals(temp3))
				if((null==temp2||"".equals(temp2))&&(null==temp1||"".equals(temp1))){
					hql+=" where "+temp3;

				}else
				hql+=conTwoThree+" "+temp3+" ";
		}
		if(null!=listFour){
			temp4=this.assemblyHQLCondition(listFour);
			if(null!=temp4  && !"".equals(temp4))
				if((null==temp3||"".equals(temp3))&&(null==temp2||"".equals(temp2))&&(null==temp1||"".equals(temp1))){
					hql+=" where "+temp4;
				}else
				hql+=conThreeFour+" "+temp4+" ";
		}
		if (orderPropertyName != null && orderPropertyName.length > 0) {
			hql = hql + " order by  ";
			for (int i = 0; i < orderPropertyName.length; i++) {

				if (i == 0) {
					hql = hql + " o." + orderPropertyName[i];
				} else {
					hql = hql + " ,o." + orderPropertyName[i];
				}

				if (orderValue != null &&null!=orderValue[i]&& !("".equals(orderValue[i].trim()))) {
					hql = hql + "  " + orderValue[i].trim();
				}

			}
		}

		return this.commonDao.findByPage(hql, pageModel);
	}


	private String assemblyHQLCondition(List list){
		/*list{String[]propertyName,String[]propertyCon,
		Object[] propertyValue, String[] condition}*/
		String resultHQL="";

		if(null!=list){
			String[] propertyName=(String[])list.get(0);
			String[] propertyCon=(String[])list.get(1);
			Object[] propertyValue=(Object[])list.get(2);
			String[] condition=(String[])list.get(3);
			if(null!=propertyValue){
				int num=0;
				String hqlTemp="";
				for(int i=0;i<propertyValue.length;i++){
					if(!(propertyValue[i]==null||("".equals(propertyValue[i].toString().trim())))){
						if("like".equals(propertyCon[i])){
							propertyValue[i]="'%"+propertyValue[i]+"%'";
						}else{
							propertyValue[i]="'"+propertyValue[i]+"'";
						}
						num++;
						if(num==1){
							hqlTemp+=" o."+propertyName[i]+" "+propertyCon[i]+" "+propertyValue[i]+" ";
						}else{
							hqlTemp+=" "+condition[i-1]+" o."+propertyName[i]+" "+propertyCon[i]+" "+propertyValue[i]+" ";
						}

					}
				}
				if(hqlTemp.equals("")){
					resultHQL="";
				}else{
					resultHQL="("+hqlTemp+")";
				}
				

			}
		}
		return resultHQL;

	}


	//@Override
	public void saveDemo(Object obj) {

		this.commonDao.saveDemo(obj);

	}

	//@Override
	public void updateDemo(Object obj) {

		this.commonDao.updateDemo(obj);

	}

	//@Override
	public boolean updateOrDeleteBySQL(String sql) {

		return this.commonDao.updateOrDeleteBysql(sql);
	}

	//@Override
	public void deleteBacthDemo(Object[] id, Object objID, Object obj) {

		this.commonDao.deleteBacthDemo(id, objID, obj);

	}

	//@Override
	public void deleteAll(Collection objs) {

		this.commonDao.deleteAll(objs);

	}

	//@Override
	public void saveOrUpdateAll(Collection objs) {

		this.commonDao.saveOrUpdateAll(objs);

	}
	
	public PageModel findByPage(final String hql, final PageModel pm){
		return commonDao.findByPage(hql, pm);
	}




}
